<template>
  <div style="padding:20px">
    <el-button
      type="primary"
      icon="el-icon-plus"
      style="margin: 10px 0px"
      @click="showDialog"
    >添加
    </el-button
    >
    <!--
         表格组件
         data:表格组件将来需要展示的数据------数组类型
         border：是给表格添加边框
         column属性
         label：显示标题
         width：对应列的宽度
         align：标题的对齐方式
         prop:对应列内容的字段名
         注意1：elmentUI当中的table组件，展示的数据是以一列一列进行展示数据
       -->
    <el-table style="width: 100%" border :data="list">
      <el-table-column type="index" label="序号" width="80px" align="center">
      </el-table-column>
      <el-table-column prop="categoryName" label="分类名称" align="center">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center" width="180px" sortable>
      </el-table-column>
      <el-table-column prop="updateTime" label="修改时间" align="center"  width="180px" sortable>
      </el-table-column>
      <el-table-column prop="prop" label="操作" width="300px" align="center">
        <template v-slot="{row}">
          <el-button
            type="warning"
            icon="el-icon-edit"
            size="mini"
            @click="updateCategory(row)"
          >修改
          </el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="deleteCategory(row)"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :title="cateInfo.categoryId ? '修改商品' : '添加商品'"
               :visible.sync="dialogFormVisible"
               style="width: 1200px; margin: 0 auto;"
    >
      <el-form style="width: 80%" :rules="rules" ref="ruleForm" :model="cateInfo">
        <el-form-item label="商品名称" label-width="100px" prop="categoryName">
          <el-input autocomplete="off" v-model="cateInfo.categoryName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateCategory"
        >确 定
        </el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'index',
  data() {
    return {
      list: [],
      cateInfo: {
        categoryName: ''
      },
      rules: {
        categoryName: [
          { required: true, message: '分类不能为空', trigger: 'blur' }
        ]
      },
      dialogFormVisible: false
    }
  },
  methods: {
    // 获取分类数据
    async getCategoryList() {
      let result = await this.$Request('/category/get')
      if (result.code === 200) {
        this.list = result.data
      }
    },
    // 修改分类
    updateCategory(row) {
      // 显示对话框
      this.dialogFormVisible = true
      // 数据引入
      this.cateInfo = { ...row }
    },
    // 删除分类
    deleteCategory(row) {
      this.$message.info('该功能敬请期待')
    },
    //点击添加品牌的按钮
    showDialog() {
      //显示对话框
      this.dialogFormVisible = true
      //清除数据
      this.cateInfo = {
        categoryName: ''
      }
    },
    // 增加或修改分类
    async addOrUpdateCategory() {
      let result = await this.$Request('/category/addOrUpdate', {
        ...this.cateInfo
      })
      if (result.code === 200) {
        if (this.cateInfo.categoryId) {
          this.$message.success('修改分类名称成功')
        }else{
          this.$message.success('添加分类成功')
        }
        // 隐藏对话框
        this.dialogFormVisible = false
        // 重新获取数据
        this.getCategoryList();
      }else {
        this.$message.info(result.message)
      }

    }
  },
  mounted() {
    this.getCategoryList()
  }
}
</script>

<style scoped>

</style>
